<?php
class BButtonColumn extends CButtonColumn {
	public $viewButtonOptions=array('icon_class'=>'icon-search');
	public $updateButtonOptions=array('icon_class'=>'icon-edit');
	public $deleteButtonOptions=array('icon_class'=>'icon-remove','class'=>'remove');
	
	protected function renderButton($id, $button, $row, $data) {
		if (isset ( $button ['visible'] ) && ! $this->evaluateExpression ( $button ['visible'], array (
				'row' => $row,
				'data' => $data 
		) ))
			return;
		$label = isset ( $button ['label'] ) ? $button ['label'] : $id;
		$url = isset ( $button ['url'] ) ? $this->evaluateExpression ( $button ['url'], array (
				'data' => $data,
				'row' => $row 
		) ) : '#';
		$options = isset ( $button ['options'] ) ? $button ['options'] : array ();
		
		
		echo CHtml::link ( CHtml::tag('i',array('class'=>$options['icon_class']),'',true) , $url,$options);
	}
	
	/**
	 * Initializes the default buttons (view, update and delete).
	 */
	protected function initDefaultButtons()
	{
		if($this->viewButtonLabel===null)
			$this->viewButtonLabel=Yii::t('zii','View');
		if($this->updateButtonLabel===null)
			$this->updateButtonLabel=Yii::t('zii','Update');
		if($this->deleteButtonLabel===null)
			$this->deleteButtonLabel=Yii::t('zii','Delete');
		
		if($this->deleteConfirmation===null)
			$this->deleteConfirmation=Yii::t('common','你是否想删除该项?');
	
		foreach(array('view','update','delete') as $id)
		{
			$button=array(
					'label'=>$this->{$id.'ButtonLabel'},
					'url'=>$this->{$id.'ButtonUrl'},
					'imageUrl'=>$this->{$id.'ButtonImageUrl'},
					'options'=>$this->{$id.'ButtonOptions'},
			);
			if(isset($this->buttons[$id]))
				$this->buttons[$id]=array_merge($button,$this->buttons[$id]);
			else
				$this->buttons[$id]=$button;
		}
	
		
		
		if(!isset($this->buttons['delete']['click']))
		{
			if(is_string($this->deleteConfirmation))
				$confirmation="if(!confirm(".CJavaScript::encode($this->deleteConfirmation).")) return false;";
			else
				$confirmation='';
	
			if(Yii::app()->request->enableCsrfValidation)
			{
				$csrfTokenName = Yii::app()->request->csrfTokenName;
				$csrfToken = Yii::app()->request->csrfToken;
				$csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
			}
			else
				$csrf = '';
	
			if($this->afterDelete===null)
				$this->afterDelete='function(){}';
			$this->buttons['delete']['click']=<<<EOD
function() {
	$confirmation
	var th=this;
	var afterDelete=$this->afterDelete;
	$.fn.yiiGridView.update('{$this->grid->id}', {
		type:'POST',
		url:$(this).attr('href'),$csrf
		success:function(data) {
			$.fn.yiiGridView.update('{$this->grid->id}');
			afterDelete(th,true,data);
		},
		error:function(XHR) {
			return afterDelete(th,false,XHR);
		}
	});
	return false;
}
EOD;
		}
	}
	/**
	 * Registers the client scripts for the button column.
	 */
	protected function registerClientScript()
	{
		$js=array();
		foreach($this->buttons as $id=>$button)
		{
			if(isset($button['click']))
			{
				$function=CJavaScript::encode($button['click']);
				$class=preg_replace('/\s+/','.',$button['options']['class']);
				$js[]="$(document).on('click','#{$this->grid->id} a.{$class}',$function);";
			}
		}
	
		if($js!==array())
			Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id, implode("\n",$js));
	}
}

?>